package com.atguigu.service.impl;

import com.atguigu.mapper.OaProcessTemplateMapper;
import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.model.process.ProcessType;
import com.atguigu.service.OaProcessTemplateService;
import com.atguigu.service.OaProcessTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OaProcessTemplateServiceImpl extends ServiceImpl<OaProcessTemplateMapper, ProcessTemplate> implements OaProcessTemplateService {
    @Autowired
    private OaProcessTemplateMapper oaProcessTemplateMapper;
    @Autowired
    private OaProcessTypeService oaProcessTypeService;

    @Override
    public IPage<ProcessTemplate> selectPage(IPage<ProcessTemplate> page1) {
        IPage<ProcessTemplate> processTemplateIPage = oaProcessTemplateMapper.selectPage(page1, null);
        List<ProcessTemplate> records = processTemplateIPage.getRecords();
        for (ProcessTemplate record : records) {
            ProcessType processType = oaProcessTypeService.getById(record.getProcessTypeId());
            record.setProcessTypeName(processType.getName());
        }
        processTemplateIPage.setRecords(records);
        return processTemplateIPage;
    }

    @Override
    public List<ProcessTemplate> findTemplateByTypeId(Long processTypeId) {
        LambdaQueryWrapper<ProcessTemplate> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcessTemplate::getProcessTypeId,processTypeId);
        return baseMapper.selectList(queryWrapper);
    }
}
